<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单输入 ----------------------------------->
      <el-form
        :rules="loginrules"
        :model="loginForm"
        label-width="0px"
        class="login_form"
        ref="loginRef"
      >
        <!-- 登录名字 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 登录密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima "
            type="password"
          ></el-input>
        </el-form-item>

        <!-- 按钮区 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script >
export default {
  name: 'Login',
  data () {
    return {
      // 登录的内容表单
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 登录效验 可以在element 组件里面查看
      loginrules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 7, message: '长度在 3 到 7个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 重置-------
    resetForm () {
      // 在this里面去找让他重置
      this.$refs.loginRef.resetFields()
    },
    // 登录-------
    login () {
      // 拿到表单数据
      this.$refs.loginRef.validate(async (res) => {
        // 格式验证失败
        if (!res) return
        // 前面 配合使用 await async 可以不then 就的到数据
        const { data: resdate } = await this.$http.post('login', this.loginForm)
        if (resdate.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        // 用 sessionStorage 保存到浏览器缓存区
        window.sessionStorage.setItem('token', resdate.data.token)
        // 跳转 页面 其实是 本地跳转
        this.$router.push('/home')
      })
    }
  },
  components: {}
}
</script>

<style  lang="less" scoped>
.login_container {
  background: url('../assets/2.jpg') no-repeat center;

  background-size: cover;

  // background-color: aqua;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 30px;
  position: absolute;
  left: 75%;
  top: 50%;
  transform: translate(-50%, -50%);
  opacity: 0.9;
}
.avatar_box {
  width: 130px;
  height: 130px;
  position: absolute;
  border: 1px solid #eee;
  box-shadow: 0 0 10px #ddd;
  background-color: #fff;
  border-radius: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
